﻿Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Globalization

Public Class Coefficients
    Private coeffpath As String
    Private coeff As List(Of CoeffObject)
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        coeffpath = Directory.GetCurrentDirectory + "\coeff.txt"

        If File.Exists(coeffpath) Then
            coeff = TextSerializer.DeserializeCoeff(coeffpath)
        Else
            coeff = New List(Of CoeffObject)
            For Each s As String In CacheOperation.types
                coeff.Add(New CoeffObject(s))
            Next
        End If

        Me.CoeffsBindingSource.DataSource = coeff
    End Sub

    Private Sub Coefficients_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        TextSerializer.SerializeCoeff(coeffpath, coeff)
    End Sub


    Private Sub Coefficients_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub DataGridView1_CellParsing(sender As Object, e As DataGridViewCellParsingEventArgs) Handles DataGridView1.CellParsing
        If e.ColumnIndex > 0 Then
            Try
                e.Value = Variables.HardConvertToDouble(e.Value)
                e.ParsingApplied = True
            Catch ex As ArgumentException
                e.ParsingApplied = False
            End Try
        End If
    End Sub

    Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        e.Cancel = True
    End Sub
End Class